<?php

class Annouce
{
    private $apiurl;
    private $apis;
    private $action;//$req['op']跳转的函数
    private $id    		= 0;//公告id
    private $start 		= 0;//分页开始位置
    private $length 	= 20;//分页长度
    private $title		= '';//公告标题
    private $content 	= '';//公告文字内容
    private $url		= '';//公告跳转的链接  
    private $picture    = ''; //公告图片链接
    private $online	    = 1 ;//公告上线
    private $test		= 0 ;//1是测试，0是正式
    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
           'getAnnounceList','removeAnnounce','sendAnnounce','setAnnouceOnLine'
        );
    }
    
    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;
    
        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }
    
    private function setAnnouceOnLine($req){
    
    	$param = $this->filterArgumentsOfPassed($req);
    	
    	$res   = requestApi($param);
    	 
    	if($res){
    		return 1;
    	}
    	return 0;
    }
    private function sendAnnounce($req){
    	$param = $this->filterArgumentsOfPassed($req);
    	 
    	$res   = requestApi($param);
    	
    	if($res){
    		return 1;
    	}
    	return 0;
    }
    private function getAnnounceList($req){
    	$param = $this->filterArgumentsOfPassed($req);
    	
    	$list   = requestApi($param);
    	
    	return render('announce',array('list'=>$list,'pageUrl'=>$this->pageUrl($req)));
    }
    private function removeAnnounce($req){
    	$param = $this->filterArgumentsOfPassed($req);
    	$res   = requestApi($param);
    	if($res){
    		return 1;
    	}
    	return 0;
    }
 
    /**
     * 过滤传参
     * @param unknown $req
     * @return multitype:string unknown Ambigous <number, unknown> Ambigous <string, unknown> NULL number
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case 'getAnnounceList':
            	$param = array(
            			'start'    =>$this->validateParam('start',$req['start']),
            			'length'   =>$this->validateParam('length',$req['length']),
            			'do'       => 'getAnnounceList',
            			'format'   => 'json',
            	);
                break;
            case 'removeAnnounce':
            	$param = array(
            			'id' 	   => $this->validateParam('id',$req['id']),
            			'test'	   => $this->validateParam('test',$req['test']),
            			'do'       => 'removeAnnounce',
            			'format'   => 'json',
            	);
                break;
            case 'sendAnnounce':         
            	$title	   		  =  $this->validateParam('title',strval($req['title']));
            	$message  		  =  $this->validateParam('content',trim(urlencode($req['content'])));
            	$action		      =  $this->validateParam('url', trim(urlencode($req['action'])));
            	$picture		  =  trim($req['picture']);            	
            	if(!empty($picture)){
            		$picture		  = explode(',', $picture);
            		$picture		  = json_encode($picture);
            	}
          
            	$param = array(
            			'do'       => 'adminSendAnnounce',
            			'format'   => 'json',            		
            			'title' 	=> $title,
            			'message'	=> $message,
            			'picture'	=> $picture,
            			'action'	=> $action,         		
            	);
            	break;
             case 'setAnnouceOnLine':
             	$param = array(
            		'do'       => 'setAnnouceOnLine',
            		'format'   => 'json',            		
            		'online'   => $this->online	
            	);
             	break;
                             
        }
    
        return $param;
    }
    private function validateParam($strParam,$reqParam){
    	return empty($reqParam) ? $this->$strParam : $reqParam;
    }
    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
    	$mpurl   = 'admin.php?ac=sendAnnounce_class&op=getAnnounceList'; 

		$start = $this->validateParam('start',$req['start']);
		
        $pageurl = multi($start, $this->length, $mpurl);
      
        return $pageurl;
    }
}

$req = request();
$annouce  = new Annouce($_SC ['apiurl']);
echo $annouce->execute($req['op']);
exit;

